import concurrent.futures
import json
import os
import time

import requests

# 获取当前 Python 文件的路径
current_dir = os.path.dirname(os.path.abspath(__file__))

# 指定列表 JSON 文件所在的文件夹路径
folder_path = 'd:\\Project\\Python\\fang\\resource\\list_json'

# 创建保存结果的文件夹
result_folder = 'd:\\Project\\Python\\fang\\resource\\detail_json'

# 自定义请求头
headers = {
    'accept': 'application/json, text/javascript, */*; q=0.01',
    'request-from': '1',
    'Origin': 'https://erp.fangline.cn',
    'x-requested-with': 'XMLHttpRequest',
    'page-name': '/visit-house/house-list',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36',
    'cookie': 'SERVERID=9d68c5451939286014101f3f74e005c5|1714476458|1714468549;Path=/; JSESSIONID=665b1c39-b4e5-4d7d-b5e4-f3c919e37da6; SERVERID=9d68c5451939286014101f3f74e005c5|1714476568|1714468549; guideStatus=6',
    'Postman-Token': '955e98fd-a871-42e6-9035-b552be73cba0',
    'Host': 'erp.fangline.cn',
    'Accept-Encoding': 'gzip, deflate, br',
    'Connection': 'keep-alive',
    'Content-Length': '0'
}

os.makedirs(result_folder, exist_ok=True)


def process_record(record):
    try:
        house_id = record['houseId']
        result_filename = f'{result_folder}\\{house_id}.json'

        # 判断文件是否已存在
        if os.path.exists(result_filename):
            print(f'File for houseId {house_id} already exists. Skipping...')
            return

        url = f'http://erp.fangline.cn/house/get-house-details-by-id.json?houseId={house_id}'

        # 发送 HTTP 请求
        response = requests.post(url, headers=headers)

        # 创建保存结果的文件名
        result_filename = f'{result_folder}/{house_id}.json'

        # 将响应结果保存到文件
        with open(result_filename, 'w') as result_file:
            result_file.write(response.text)

        # 打印保存结果的文件路径
        print(f'Saved response for houseId {house_id} in file: {result_filename}')
    except Exception as e:
        print(e)



for filename in os.listdir(folder_path):
    """循环读取文件夹中的 JSON 文件
    """
    if filename.endswith('.json'):
        file_path = os.path.join(folder_path, filename)

        # 读取文件中的 JSON 数据
        with open(file_path, 'r', encoding='utf-8') as file:
            json_data = json.load(file)

        # 获取 records 集合
        records = json_data['page']['records']

        # 使用线程池来处理请求
        with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
            executor.map(process_record, records)
